#include  	"main.h"
#include 	"system.h"



uint8 NB_NextSocketNumber=0 ;



void BC26_Power()
{
	printf("--->>BC26 power on...\n");
	BC26HardwareReset();
	BC26_Reset_Low ;
	BC26_POWKEY_High ;
	NB_Delay(1000);
	BC26_POWKEY_Low ;
	NB_Delay(100);
	BC26_Reset_High ;
	NB_Delay(1000);
	BC26_Reset_Low ;
	NB_Delay(1000);
}


void BC26Initial(void)
{
	GPIO_InitTypeDef GPIO_InitStruct ;

	GPIO_InitStruct.Pin   = MCU_POWKEY_PIN;
	GPIO_InitStruct.Mode  = GPIO_MODE_OUTPUT_PP;
	GPIO_InitStruct.Pull  = GPIO_NOPULL;
	GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
	HAL_GPIO_Init(MCU_POWKEY_PORT, &GPIO_InitStruct);

	GPIO_InitStruct.Pin   = MCU_RESET_PIN;
	GPIO_InitStruct.Mode  = GPIO_MODE_OUTPUT_PP;
	GPIO_InitStruct.Pull  = GPIO_NOPULL;
	GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
	HAL_GPIO_Init(MCU_RESET_PORT, &GPIO_InitStruct);

	GPIO_InitStruct.Pin   = NB_PW_EN_PIN;
	GPIO_InitStruct.Mode  = GPIO_MODE_OUTPUT_PP;
	GPIO_InitStruct.Pull  = GPIO_NOPULL;
	GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
	HAL_GPIO_Init(NB_PW_EN_PORT, &GPIO_InitStruct);
	NB_PW_Disable;

	
	BC26_Power();
	
}


uint8 BC26_ATI_Status(void)
{
    UINT8 BC26_ATI_cmd[5+1]={'A','T','I','\r','\n',0};
    UINT8 i=10;
    printf("\n--->>>BC26_ATI_Status...\n");
    while(i--)
    {
    	
        gNB_ReceData->len=0 ;
        SendDataToNBModule(BC26_ATI_cmd,5);
        NB_Delay(1000);
        printf("len=%d\n",gNB_ReceData->len);
        gNB_ReceData->buf[gNB_ReceData->len]=0 ;
        printf("%s\n",gNB_ReceData->buf);

		printf("%c%c%c%c%c\n",gNB_ReceData->buf[0],gNB_ReceData->buf[1],gNB_ReceData->buf[2],\
			gNB_ReceData->buf[3],gNB_ReceData->buf[4]);
        
        if((strncmp(gNB_ReceData->buf,BC26_ATI_cmd,4)==0)&&\
				(strstr(gNB_ReceData->buf,"OK")!=NULL))
        {
            printf("BC26_ATI_Status is OK\n");
            return TRUE ;
        }

        
    }

	printf("BC26_ATI_Status is ERR\n");
	return FALSE;
}


void BC26_CFUN(void)
{
	uint8_t BC26_CFUN_cmd[10+1]={'A','T','+','C','F','U','N','?','\r','\n'};  
    UINT8 i=5;
    printf("\n--->>>Set UE Functionality...\n");
    while(i--)
    {
        gNB_ReceData->len=0 ;
        SendDataToNBModule(BC26_CFUN_cmd,10);
        NB_Delay(1000);
        printf("len=%d\n",gNB_ReceData->len);
        
        gNB_ReceData->buf[gNB_ReceData->len]=0 ;
        printf("%s\n",gNB_ReceData->buf);
        
        if((strncmp(gNB_ReceData->buf,BC26_CFUN_cmd,9)==0)&&\
        (strstr(gNB_ReceData->buf,"OK")!=NULL))
        {
            printf("BC26_CFUN is OK\n");
            return ;
        }
        else 
        {
            printf("BC26_CFUN is ERR\n");
        }
        
    }
    
}
uint8 BC26_Request_ICCID(void)
{
	uint8_t BC26_Check_CIMI_cmd[10+1]={'A','T','+','Q','C','C','I','D','\r','\n'};
    UINT8 i=5;
    printf("--->>>Request_ICCID...\n");
    
    while(i)
    {
        gNB_ReceData->len=0 ;
        SendDataToNBModule(BC26_Check_CIMI_cmd,9);
        NB_Delay(1000);
        printf("len=%d\n",gNB_ReceData->len);
        
        gNB_ReceData->buf[gNB_ReceData->len]=0 ;
        printf("%s\n",gNB_ReceData->buf);
        
        if((strncmp(gNB_ReceData->buf,BC26_Check_CIMI_cmd,8)==0)&&\
        (strstr(gNB_ReceData->buf,"OK")!=NULL))
        {
        	UtilGetStringFlag(gNB_ReceData->buf,"+QCCID:","\r\n",collectData.BC26_ICCID);
					printf("BC26_ICCID=%s\n",collectData.BC26_ICCID);
          return	TRUE;
        }

        --i;
    }
	if(i==0)
	{
		printf("\n\nNO SIM Card\n");
	}
    return FALSE ;
}

uint8 BC26_Request_IMEI(void)
{
	uint8_t BC26_CGSN_cmd[11+1]={'A','T','+','C','G','S','N','=','1','\r','\n'};  
    UINT8 i=5;
    printf("\n--->>>Request IMEI...\n");
    
    while(i--)
    {
        gNB_ReceData->len=0 ;
        SendDataToNBModule(BC26_CGSN_cmd,11);
        NB_Delay(1000);
        printf("len=%d\n",gNB_ReceData->len);
        
        gNB_ReceData->buf[gNB_ReceData->len]=0 ;
        printf("%s\n",gNB_ReceData->buf);
        
        if((strncmp(gNB_ReceData->buf,BC26_CGSN_cmd,10)==0)&&\
        (strstr(gNB_ReceData->buf,"OK")!=NULL))
        {

			UtilGetStringFlag(gNB_ReceData->buf,"+CGSN:","\r\n",collectData.BC26_IMEI);
			printf("BC26_IMEI=%s\n",collectData.BC26_IMEI);
					
            return TRUE ;
        }
        else 
        {
            printf("read IMEI error\n");
        }
        
    }
    
    printf("read IMEI error\n");
    return FALSE ;
}

uint8 BC26_Active_Net(void)
{
	uint8_t BC26_CGATT_cmd[12+1]={'A','T','+','C','G','A','T','T','=','1','\r','\n'};  	  
    UINT8 i=5;
    printf("\n--->>>BC26_Active_Net...\n");
    while(i--)
    {
        gNB_ReceData->len=0 ;
        SendDataToNBModule(BC26_CGATT_cmd,12);
		NB_Delay(2000);
		
		
        printf("len=%d\n",gNB_ReceData->len);
        gNB_ReceData->buf[gNB_ReceData->len]=0 ;
        printf("%s\n",gNB_ReceData->buf);
        
        
        if((strncmp(gNB_ReceData->buf,BC26_CGATT_cmd,11)==0)&&\
        (strstr(gNB_ReceData->buf,"OK")!=NULL))
        {
            printf("BC26_Active_Net is OK\n");
            return TRUE;
        }
		else
		{
			NB_Delay(2000);
			NB_Delay(2000);
			NB_Delay(1000);
		
		}
		
        
    }
    
    printf("BC26_Active_Net is ERR\n");
    return FALSE ;
}
uint8 Check_BC26_Net_Status(void)
{
	
	uint8_t BC26_Check_CGATT_cmd[11+1]={'A','T','+','C','G','A','T','T','?','\r','\n'};
    UINT8 i=5,result=0 ;
    printf("\n--->>>Check_BC26_Net_Status...\n");
    while(i--)
    {
        /*******************************************
    		Query whether the network is activated: 
    		1 means attached to network successfully, 
    		while 0 means has not been attached to network.
        *******************************************/
        gNB_ReceData->len=0 ;
        SendDataToNBModule(BC26_Check_CGATT_cmd,11);
        NB_Delay(2000);
		
        printf("len=%d\n",gNB_ReceData->len);
        
        
        gNB_ReceData->buf[gNB_ReceData->len]=0 ;
        printf("%s\n",gNB_ReceData->buf);
        
        if((strncmp(gNB_ReceData->buf,BC26_Check_CGATT_cmd,10)==0)&&\
        (strstr(gNB_ReceData->buf,"OK")!=NULL))
        {
            result=UtilGetIntFlag(gNB_ReceData->buf,"+CGATT:","\r\n");
            printf("Check_BC26_Net_Status is OK=%d\n",result);
            return result ;
        }
        else 
        {
            printf("Check_BC26_Net_Status is ERR\n");
        }
    }
    
    return FALSE ;
}

void BC26_Check_Band(void)
{
    
	uint8_t BC26_Check_Band_cmd[11+1]={'A','T','+','Q','B','A','N','D','?','\r','\n'};
    UINT8 i=5;
    printf("\n--->>>Query the list of supported bands...\n");
    while(i--)
    {
        gNB_ReceData->len=0 ;
        SendDataToNBModule(BC26_Check_Band_cmd,11);
        NB_Delay(1000);
        printf("len=%d\n",gNB_ReceData->len);
        
        gNB_ReceData->buf[gNB_ReceData->len]=0 ;
        printf("%s\n",gNB_ReceData->buf);
        
        if((strncmp(gNB_ReceData->buf,BC26_Check_Band_cmd,10)==0)&&\
        (strstr(gNB_ReceData->buf,"OK")!=NULL))
        {
            printf("BC26_Check_Band is OK\n");
            return ;
        }
        else 
        {
            printf("BC26_Check_Band is ERR\n");
        }
    }
    
}

uint8 BC26_Check_CESQ(void)
{
    
	uint8_t BC26_Check_CESQ_cmd[9+1]={'A','T','+','C','E','S','Q','\r','\n'};
    UINT8 i=5,result=0 ;
    printf("\n--->>>Query the signal strength...\n");
    while(i--)
    {
        gNB_ReceData->len=0 ;
        SendDataToNBModule(BC26_Check_CESQ_cmd,9);
        NB_Delay(1000);
        printf("len=%d\n",gNB_ReceData->len);
        
        gNB_ReceData->buf[gNB_ReceData->len]=0 ;
        printf("%s\n",gNB_ReceData->buf);
        
        
        
        
        if((strncmp(gNB_ReceData->buf,BC26_Check_CESQ_cmd,8)==0)&&\
        (strstr(gNB_ReceData->buf,"OK")!=NULL))
        {
            
            result=UtilGetIntFlag(gNB_ReceData->buf,"+CESQ:",",");
            printf("BC26_Check_CESQ is OK=%d\n",result);
            return result ;
        }
        else 
        {
            printf("BC26_Check_CESQ is ERR\n");
        }
    }
    return 0 ;
}

int BC26_Register_Net_Status(void)
{
    /*********************************************************
    AT+CEREG? ��ѯ NB ����ע��״̬����
    +CEREG: < n>,< state>,[,< lac>,< rac>,< ci>[,< AcT>]]
    ����˵����< n>:
    0 �ر�����ע��������ϱ������
    1 ������ע��������ϱ������+CREG:
    2 ������ע���λ����Ϣ�������ϱ������+CEREG: [,[,]]
    < state>:NB ע��״̬
    0 δע�ᣬMT ��ǰ��ǰû����������ע�ᵽ��Ӫ������
    1 ��ע�ᣬע�ᵽ��������
    2 δע�ᣬ���� MT ��ʱ���ڳ��Ը��Ż�����������ע�����Ӫ������
    3 ע�ᱻ�ܾ�
    4 δ֪����
    5 ��ע�ᣬע�ᵽ��������
    **********************************************************/
    
	uint8_t BC26_Register_Net_Status_cmd[11+1]={'A','T','+','C','E','R','E','G','?','\r','\n'};
    UINT8 i=5,result=0 ;
    
    printf("\n--->>>��ѯ NB ����ע��״̬\n");
    while(i--)
    {
        gNB_ReceData->len=0 ;
        SendDataToNBModule(BC26_Register_Net_Status_cmd,11);
        NB_Delay(2000);
		
        printf("len=%d\n",gNB_ReceData->len);
        
        gNB_ReceData->buf[gNB_ReceData->len]=0 ;
        printf("%s\n",gNB_ReceData->buf);
        
        if((strncmp(gNB_ReceData->buf,BC26_Register_Net_Status_cmd,10)==0)&&\
        (strstr(gNB_ReceData->buf,"OK")!=NULL))
        {
            
            result=UtilGetIntFlag(gNB_ReceData->buf,",","\r\n");
            printf("BC26_Register_Net_Status is OK=%d\n",result);
			if(result==1)
			{
            	return result ;
			}

        }
        else 
        {
         	NB_Delay(2000);
			NB_Delay(2000);
			
			
			
        }
    }
    printf("BC26_Register_Net_Status is ERR\n");
    return FALSE ;
    
}


uint8 BC26_CSCON(void)
{
    
    
	uint8_t BC26_CSCON_cmd[11+1]={'A','T','+','C','S','C','O','N','?','\r','\n'};
    UINT8 i=2,result=0 ;    
    printf("\n--->>>��ѯ����״̬\n");
    while(i--)
    {
        gNB_ReceData->len=0 ;
        SendDataToNBModule(BC26_CSCON_cmd,11);
        NB_Delay(2000);
		
		
        printf("len=%d\n",gNB_ReceData->len);
        gNB_ReceData->buf[gNB_ReceData->len]=0 ;
        printf("%s\n",gNB_ReceData->buf);
        
        
        if((strncmp(gNB_ReceData->buf,BC26_CSCON_cmd,10)==0)&&\
        (strstr(gNB_ReceData->buf,"OK")!=NULL))
        {
            printf("BC26_CSCON is OK\n");
            result=UtilGetIntFlag(gNB_ReceData->buf,",","\r\n");
            if(result==1)
            {
                printf("Signalling Connection Status is Connetced\n");
				return TRUE ;
            }
            else if(result==0)
            {
                NB_Delay(2000);
				NB_Delay(2000);
				
				
				
            }
  
        }
        else 
        {
            printf("BC26_CSCON is ERR\n");
        }
        
    }
	printf("Signalling Connection Status is IDIE\n");
    return FALSE ;
    
}

void BC26_COPS(void)
{
    /*************************************************************************
    AT+COPS
    ���ܣ�����ѡ����Ӫ���硣��Ҫ��2��ѡ��ʽ��
    1.�ֹ�ģʽ(manual): ����Ӧ�ó���ָ�������硣����ɹ��������ע�ᡣ
    2.�Զ�ģʽ��automatic���������������硣���ҵ��������ע�᣻���������������硣
    3.�ֹ�/�Զ�ģʽ��manual/automatic��������Ӧ�ó���ָ������·������ɹ�����ѡ��Ӧ�ó���ָ�����������ע�᣻���������Զ�ģʽ��������
    
    ����46000��ʾ��Ӫ�̱�ʶ��460��ʾ�й���00��ʾ�й��ƶ���01��ʾ�й���ͨ
    **************************************************************************/
    
	uint8_t BC26_COPS_cmd[10+1]={'A','T','+','C','O','P','S','?','\r','\n'};
    UINT8 i=5 ;
    UINT16 result=0 ;
    uint8 startflag[3]={',','"',0};
    uint8 endflag[3]={'"',',',0};
    printf("\n--->>>��ѯ��Ӫ��\n");
    while(i--)
    {
        gNB_ReceData->len=0 ;
        SendDataToNBModule(BC26_COPS_cmd,10);
        NB_Delay(2000);
		
        printf("len=%d\n",gNB_ReceData->len);
        gNB_ReceData->buf[gNB_ReceData->len]=0 ;
        printf("%s\n",gNB_ReceData->buf);
        
        if((strncmp(gNB_ReceData->buf,BC26_COPS_cmd,9)==0)&&\
        (strstr(gNB_ReceData->buf,"OK")!=NULL))
        {
            printf("BC26_COPS is OK\n");
            result=UtilGetIntFlag(gNB_ReceData->buf,startflag,endflag);
            printf("COPS=%d\n",result);
            if(result==46000)
            {
                printf("�й��ƶ�\n");
				return ;
            }
            else if(result==46001)
            {
                printf("�й���ͨ\n");
				return ;
            }
            else if(result==46011) 
            {
                printf("�й�����\n");
				return ;
            }
			else
			{
				NB_Delay(2000);
				NB_Delay(2000);
				
				
				

			}
        }
        else 
        {
            printf("BC26_COPS is ERR\n");
        }
    }
    printf("��ѯ������Ӫ��\n");
}

void BC26_PDPACT(void)
{
    
    
	uint8_t BC26_CGPADDR_cmd[14+1]={'A','T','+','C','G','P','A','D','D','R','=','1','\r','\n'};
    UINT8 i=5 ;
    printf("\n--->>>Get the IP address of the device...\n");
    while(i--)
    {
        gNB_ReceData->len=0 ;
        SendDataToNBModule(BC26_CGPADDR_cmd,14);
        NB_Delay(2000);
		
        printf("len=%d\n",gNB_ReceData->len);
        
        gNB_ReceData->buf[gNB_ReceData->len]=0 ;
        printf("%s\n",gNB_ReceData->buf);
        
        
        if((strncmp(gNB_ReceData->buf,BC26_CGPADDR_cmd,13)==0)&&\
			(strstr(gNB_ReceData->buf,"+CGPADDR:")!=NULL)&&\
        (strstr(gNB_ReceData->buf,"OK")!=NULL))
        {
            printf("BC26_PDPACT is OK\n");
            i=0 ;
            return;
        }
        else 
        {
        	NB_Delay(4000);
			
			
			
        }
        
    }
	printf("BC26_PDPACT is ERR\n");
   
}

int8 CreatSocket(uint8 type,uint8	socketid,UINT8*serveriP,uint8*serverport,uint8*localport)
{ 
	uint8 j=0,sockedid=0,result=0 ;
    uint8 ATStrBuf[50]={0};
	const UINT8 Buf4[6]={'"','U','D','P','"',0};
	const UINT8 Buf[6]={'"','T','C','P','"',0};
	const UINT8 Buf1[6]={',','"',0 };
	const UINT8 Buf2[6]={'"',',',0};
	const UINT8 Buf3[2]={',',0 };
    uint8*p=NULL ;
    printf("\n--->>>Connect Socket to Remote Address and Port...\n");
    
	sprintf(ATStrBuf,"AT+QIOPEN=%d,%d,",1,socketid);
    if(type==TCP_Connection)
    {
    	strcat(ATStrBuf,Buf);
    }
	else  if(type==UDP_Connection)
	{
		strcat(ATStrBuf,Buf4);
	}
    else
    {
		printf("socket type err\n");
	}
    strcat(ATStrBuf,Buf1);
    
    strcat(ATStrBuf,serveriP);
    
    strcat(ATStrBuf,Buf2);
    
    strcat(ATStrBuf,serverport);
    
    strcat(ATStrBuf,Buf3);
    
    strcat(ATStrBuf,localport);
    
    strcat(ATStrBuf,Buf3);
    
    strcat(ATStrBuf,"0,");
    
    strcat(ATStrBuf,"0");
    
    strcat(ATStrBuf,"\r\n\0");
    printf("ATStrBuf=%s\n",ATStrBuf);

        gNB_ReceData->len=0 ;
        memset(gNB_ReceData->buf,0,Uart_ReceBufLen);
        SendDataToNBModule(ATStrBuf,strlen(ATStrBuf));
        j=0 ;
        while(strstr(gNB_ReceData->buf,"+QIOPEN:")==NULL)
        {
            NB_Delay(1000);
            if(++j>6)
            {
                break ;
            }
        }
		
        printf("len=%d\n",gNB_ReceData->len);
        
        gNB_ReceData->buf[gNB_ReceData->len]=0 ;
        printf("%s\n",gNB_ReceData->buf);
      
        if((strncmp(gNB_ReceData->buf,"AT+QIOPEN",strlen("AT+QIOPEN"))==0)&&\
        (strstr(gNB_ReceData->buf,"OK")!=NULL))
        {
            sockedid=UtilGetIntFlag(gNB_ReceData->buf,"+QIOPEN:",",");
            printf("socket %d\n",sockedid);
            p=strstr(gNB_ReceData->buf,"+QIOPEN:");
            if(p!=NULL)
            {
                result=UtilGetIntFlag(p,",","\r\n");
                printf("result %d\n",result);
                if(result==0)
                {
                    printf("creak socket %d is OK\n",sockedid);
                    
                    return sockedid ;
                }
            }
            
			

			
            
        }
        else 
        {
			printf("ERR\n");
        }
        
        
		
    
    return-1 ;
    
}


UINT8	BC26_ConfigSleepMode(uint8		mode)
{

	uint8_t BC26_QSCLK0_cmd[14+1]={'A','T','+','Q','S','C','L','K','=','0','\r','\n'};
	uint8_t BC26_QSCLK1_cmd[14+1]={'A','T','+','Q','S','C','L','K','=','1','\r','\n'};
	UINT8	buffer[20]={0};
	UINT8 i=3;
	if(mode==BC26_DisableSleepMode)
	{
		memcpy(buffer,BC26_QSCLK0_cmd,12);
	}
	else if(mode==BC26_EnableSleepMode)
	{
		memcpy(buffer,BC26_QSCLK1_cmd,12);
	}
	else
	{
		printf("BC26_ConfigSleepMode err\n");
		return FALSE;
	}
	printf("\n--->>>�������� %d\n",mode);
	while(i--)
	{
		gNB_ReceData->len=0 ;
		SendDataToNBModule(buffer,13);
		NB_Delay(1000);
		
		printf("len=%d\n",gNB_ReceData->len);
		
		
		gNB_ReceData->buf[gNB_ReceData->len]=0 ;
		printf("%s\n",gNB_ReceData->buf);
		
		
		if((strncmp(gNB_ReceData->buf,"AT+QSCLK",strlen("AT+QSCLK"))==0)&&\
		(strstr(gNB_ReceData->buf,"OK")!=NULL))
		{
			printf("BC26_ConfigSleepMode is OK\n");

			return TRUE;
		}

	}

	printf("BC26_ConfigSleepMode is ERR\n");
	return FALSE ;

}
uint8 CloseSocket(uint8 socketid)
{
    uint8 i=3;
    uint8 ATStrBuf[50]={0};
    printf("\n--->>>Close Socket...\n");
    
    sprintf(ATStrBuf,"AT+QICLOSE=%d\r\n",socketid);
    while(i--)
    {
        gNB_ReceData->len=0 ;
        SendDataToNBModule(ATStrBuf,strlen(ATStrBuf));
        NB_Delay(2000);
		
        printf("len=%d\n",gNB_ReceData->len);
        
        gNB_ReceData->buf[gNB_ReceData->len]=0 ;
        printf("%s\n",gNB_ReceData->buf);
        
        if((strncmp(gNB_ReceData->buf,"AT+QICLOSE",strlen("AT+QICLOSE"))==0)&&\
        (strstr(gNB_ReceData->buf,"OK")!=NULL))
        {
            printf("Close Socket	success\n");
            return TRUE ;
        }
        else 
        {
            printf("Close Socket	ERR\n");
        }
        
        
    }
    return FALSE ;
    
}

/***********
AT+QCSEARFCN
+QCSEARFCN: 0

OK

0 Successful
2 Failure
3 Not found
***********/
uint8 BC26_QCSEARFCN(void)
{
	uint8_t BC26_CGPADDR_cmd[14+1]={'A','T','+','Q','C','S','E','A','R','F','C','N','\r','\n'};
    UINT8 i=3,result;
    printf("\n--->>>Clear NB-IoT Stored EARFCN List\n");
    while(i--)
    {
        gNB_ReceData->len=0 ;
        SendDataToNBModule(BC26_CGPADDR_cmd,14);
		
        NB_Delay(1000);
		
        printf("len=%d\n",gNB_ReceData->len);
        gNB_ReceData->buf[gNB_ReceData->len]=0 ;
        printf("%s\n",gNB_ReceData->buf);
        
        if((strncmp(gNB_ReceData->buf,BC26_CGPADDR_cmd,12)==0)&&\
        (strstr(gNB_ReceData->buf,"OK")!=NULL))
        {

                result=UtilGetIntFlag(gNB_ReceData->buf,"+QCSEARFCN:","\r\n");
                printf("result=%d\n",result);
                if((result==0)||(result==3))
                {
                    printf("AT+QCSEARFCN OK\n");
                    return TRUE;
                }		

        }
        else 
        {
        	NB_Delay(2000);
			NB_Delay(2000);
			
			
			
        }
        
    }
	printf("AT+QCSEARFCNT is ERR\n");
    return FALSE; 
}


void BC26_PowerStart(void)
{
    BC26_Power();
    BC26_ATI_Status();
    BC26_ConfigSleepMode(BC26_DisableSleepMode);
	
}

uint8 BC26_JoinNet(void)
#if 0		
{
	int8	socketid=-1;
	uint8	time=0,SignalStrength=0;
	extern	UINT8	Equipment_OnLine_Flag;
	if(Equipment_OnLine_Flag==SUCCESS)
	{
		printf("����BC26\n");
		
		/*BC26_EINT3V3_High;
		HAL_Delay(30);
		BC26_EINT3V3_Low;
		HAL_Delay(1000);*/
		return SUCCESS;	

	}
	BC26_ATI_Status();	
	BC26_QCSEARFCN();
	BC26_ConfigSleepMode(BC26_DisableSleepMode);
	if(BC26_Request_ICCID()==FALSE)
	{
		return	FAIL;
	}
    BC26_Request_IMEI();

    while(1)
    {
    	if(++time>5)
    	{
			break;
		}
    	SignalStrength=BC26_Check_CESQ();
		if(SignalStrength<10)
		{
			HAL_Delay(10000);
		}
		else
		{
			break;
		}
    }
    if(BC26_Active_Net()==FALSE)
    {
			return	FAIL;
		}
    if(BC26_Register_Net_Status()==1)
    {
	    BC26_CSCON();
	    BC26_COPS();
	    BC26_PDPACT();
		BC26_QLWDEL();
		if(BC26_QLWSERV(TelecomPlatform_IP,TelecomPlatform_port)==TRUE)
		{
			BC26_QLWCONF(BC26_IMEI);
			BC26_QLWADDOBJ();
		    if(BC26_QLWOPEN()==TRUE)
		    {
				BC26_QLWCFG();
				BC26_QLWUPDATE();
				return	SUCCESS;
				
		    }
		}
    }
	return	FAIL;

}	
#else
{
	int8	socketid=-1;
	uint8	time=0;
	
	extern	UINT8	Equipment_OnLine_Flag;
	if(Equipment_OnLine_Flag==SuccessFull)
	{
		printf("����BC26\n");
		
		/*BC26_EINT3V3_High;
		HAL_Delay(30);
		BC26_EINT3V3_Low;
		HAL_Delay(1000);*/
		return SuccessFull;	

	}

    
    if(BC26_ATI_Status()==FALSE)
   	{
		

		
		
		
			printf("\n--->>>systerm reset\n");
		
		HAL_NVIC_SystemReset();
	}

	if(BC26_QCSEARFCN()==FALSE)
	{
		 
		BC26_Power();
		BC26_ATI_Status();	
		BC26_QCSEARFCN();
	}
	
	BC26_ConfigSleepMode(BC26_DisableSleepMode);
	if(BC26_Request_ICCID()==FALSE)
	{
		return	FAIL;
	}
    if(BC26_Request_IMEI()==FALSE)
    {
		return	FAIL;
	}

	collectData.GetDeviceID=1;
	SendCollectedValToDisplayBoard(IMEIValue,&collectData);
    while(1)
    {
    	if(++time>5)
    	{
			break;
		}
    	collectData.SignalStrength=BC26_Check_CESQ();
		if(collectData.SignalStrength<10)
		{
			NB_Delay(10000);
		}
		else
		{
			break;
		}
    }
    if(BC26_Active_Net()==FALSE)
    {
		return	FAIL;
	}

    if(BC26_Register_Net_Status()==1)
    {
	    BC26_CSCON();
	    BC26_COPS();
	    BC26_PDPACT();
		BC26_QLWDEL();
		if(BC26_QLWSERV(TelecomPlatform_IP,TelecomPlatform_port)==TRUE)
		{
			BC26_QLWCONF(collectData.BC26_IMEI);
			BC26_QLWADDOBJ();
		    if(BC26_QLWOPEN()==TRUE)
		    {
				BC26_QLWCFG();
				BC26_QLWUPDATE();
				return	SuccessFull;
				
		    }
		}
    }

	return	FAIL;

}
#endif

uint8 BC26_EnterSleep(void)
{

	return BC26_ConfigSleepMode(BC26_EnableSleepMode);
}

